<?php
require_once '../classes/includeAll.php';

if(isset($_GET['dev']))
    {
        require '../resources/getConnection.php'; 
        $dev = $_GET['dev'];
        $devicesClass = new Devices($connection);
        $devices =  $devicesClass->getAllDevices();  
        $deviceExist=false;
        foreach ($devices as $device) 
        { 
            if ($device->ID == $dev) {
                $deviceExist = true;
            }
        }

        if (!$deviceExist) 
            {
                header('Location: devices.php');
            }
    }else
    {
        header('Location: devices.php');
    }
    
$mode = filter_input(INPUT_GET, "mode", FILTER_SANITIZE_SPECIAL_CHARS);
$device = new Device($dev, $connection);  

if(isset($_POST['submit']))
{   
    $column = array();
    $valuesNormal = array();
    $columnContact = array();
    $valuesContact = array();

    $inventoryNumber = filter_input(INPUT_POST, "inventoryNumber", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($inventoryNumber != null || $inventoryNumber != false) 
    {
        array_push($valuesNormal, $inventoryNumber);
        array_push($column, "inventoryNumber");
    }

    $manufacturer = filter_input(INPUT_POST, "manufacturer", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($manufacturer != null || $manufacturer != false) 
    {
        array_push($valuesNormal, $manufacturer);
        array_push($column, "manufacturer");
    }

    $purchaseDate = filter_input(INPUT_POST, "purchaseDate", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($purchaseDate != null || $purchaseDate != false) 
    {
        array_push($valuesNormal, ConvertDate($purchaseDate, false));
        array_push($column, "purchaseDate");
    }

    $warranty = filter_input(INPUT_POST, "warranty", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($warranty != null || $warranty != false) 
    {
        array_push($valuesNormal, $warranty);
        array_push($column, "warranty");
    }

    $serialNumber = filter_input(INPUT_POST, "serialNumber", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($serialNumber != null || $serialNumber != false) 
    {
        array_push($valuesNormal, $serialNumber);
        array_push($column, "serialNumber");
    }

    $name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($name != null || $name != false) 
    {
        array_push($valuesNormal, $name);
        array_push($column, "name");
    }

    $supplierID = $_POST['supplierID'];
    if ($supplierID != null || $supplierID != false) 
    {

        array_push($valuesNormal, $supplierID);
        array_push($column, "supplier_ID");
    }
    //////////////////kontakt//////////
    $nameSupplier = filter_input(INPUT_POST, "nameSupplier", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($nameSupplier != null || $nameSupplier != false) 
    {
        array_push($valuesContact, $nameSupplier);
        array_push($columnContact, "name");
    }

    $contactSaleOffice = filter_input(INPUT_POST, "contactSaleOffice", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($contactSaleOffice != null || $contactSaleOffice != false) 
    {
        array_push($valuesContact, $contactSaleOffice);
        array_push($columnContact, "contactSalesOffice");
    }

    $contactService = filter_input(INPUT_POST, "contactService", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($contactService != null || $contactService != false) 
    {
        array_push($valuesContact, $contactService);
        array_push($columnContact, "contactService");
    }

    $noteSupplier = filter_input(INPUT_POST, "noteSupplier", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($noteSupplier != null || $noteSupplier != false) 
    {
        array_push($valuesContact, $noteSupplier);
        array_push($columnContact, "note");
    }

    //Room
    $device->getInfrastructure();    

    if(count($column)>0)
    {
        $device->UpdateDevice($column, $valuesNormal);
    }
    if(count($columnContact)>0)
    {
        $device->UpdateSupplier($columnContact, $valuesContact);
    }
    $roomShould = filter_input(INPUT_POST, "roomShould", FILTER_SANITIZE_SPECIAL_CHARS);
    if($roomShould != null || $roomShould != false)
    {
        $device->UpdateRoom("roomShould_ID", $roomShould);              
    }

    $macAddress = filter_input(INPUT_POST, "macAddress", FILTER_SANITIZE_SPECIAL_CHARS);
    if ($macAddress != null || $macAddress != false) 
    {
        $device->UpdateMac("macAddress", $macAddress);
    }

}
 
    if(isset($_POST['additionAdd']))
    {
       if(isset($_POST['addition']))
       {
           $insertAddition = array();
           $additionArray = $_POST['addition'];
           foreach ($additionArray as $addition) 
           {
               if($addition[0] != "" && $addition[1] != "" && $addition[2] != "")
               {
                   $device->AddAddition($addition[1], ConvertDateTime($addition[2], false), $addition[0]);
                   
                   
               }
           }
       }
        
    }
 
$device->getDefaultInformations();      
$device->getSupplierInformation();
$device->getInfrastructure();
$device->getAdditions();
$supplierClass = new Suppliers($connection);
$suppliers = $supplierClass->getAllSuppliers();
$connection->disconnectDB();
if($device->Name =="")
{
    $title = "kein Name";
}else
{
    $title = "$device->Name ";
}
$head= "
<link rel=\"stylesheet\" type=\"text/css\" href=\"../design/css/device.css\">
<script type=\"text/javascript\" src=\"../javascript/pagesJavascript/device.js\"></script>
<script type=\"text/javascript\">	
$(document).ready(function() { 
    $(\"#list\").tablesorter({ 
        // pass the headers argument and assing a object 
        headers: { 
            // assign the secound column (we start counting zero) 
            3: { 
                sorter: false 
            }
        } 
    }); 
});
$(document).ready(function() { 
    $(\"#listAddition\").tablesorter({ 
        // pass the headers argument and assing a object 
        headers: { 
            // assign the secound column (we start counting zero) 
            3: { 
                sorter: false 
            }
        } 
    }); 
});
</script>
        ";
require_once '../menu/head.php';

$flagMeaning = (new allFlags())->DeviceFlags;
$search = array ("-", "ae",     "ue",     "oe" ,"Ae");
$replace = array(" ", "&auml;", "&uuml;", "&ouml;", "&Auml;");
$deviceChange='<tr><td></td><td><input type="checkbox" onclick="checkboxChanged()" id="checkbox" class="lastCheckboxElement"><span class=\"fontOne\">Alle ausw&auml;hlen</span></td></tr>';
$imgChange="";
if($device->ChangeFlag !=0)
{
   $i=0;
  foreach ($flagMeaning as $key => $value) {
    $i++;
    if(($device->ChangeFlag & $value)>0)
    {
      $str= str_replace($search, $replace, $key);
      if(strpos($str, "fehlgeschlagen")!==false || strpos($str, "vorhanden")!==false)
      {
        $imgChange='<img src="../design/pictures/error.png" width="16" alt="X"/>&nbsp;';
      }
      else if(strpos($str, "&Auml;nderung")!==false)
      {
        $imgChange='<img src="../design/pictures/warning.png" width="16" alt="!"/>&nbsp;';
      }
      else
      {
        $imgChange='<img src="../design/pictures/exclamation.png" width="16" alt=""/>&nbsp;';
      }
      $deviceChange.='<tr><td></td><td><input type="checkbox" name="alertTable['.$i.']" value="'. $key. '" class="alertCheckbox"/>&nbsp;' .$imgChange.'&nbsp;'. $str . '</td></tr>';
    }
  }
  $deviceChange.= '<tr><td></td><td><input type="button" value="Best&auml;tigen" name="deleteFlags" onclick="deleteChanges('.$device->ID.')" class="lastCheckboxElement" id="deleteFlags"/></td></tr>';
      
}
else 
{
  $deviceChange = "<tr><td></td><td>Keine &Auml;nderungen</td></tr>";
}

//MAIN Device
if($mode != 1 && $mode != 2 || !$mode)
{
if($device->Name =="")
{
    $tempName = "kein Name vorhanden";
}else
{
    $tempName = "$device->Name ";
}
echo" 
<form action=\"device.php?dev=$dev\" method=\"POST\">
    <h1>Ger&aumlt: <span name='name' id=\"name\">". $tempName."</span> <img onclick=\"deleteDevice(this, ". $device->ID .")\" width=\"20px\" src=\"../design/pictures/delete.png\"></h1>
    <table id=\"deviceTable\">
        <tr>
            <td>
            <span class=\"headlineTwo\">Allgemein: </span><br>
                <table class=\"informationTable\" >
                    <tr>
                    <td><span class=\"fontOne\">Typ: ".$device->DeviceType ."</h2></span></td>
                    <td></td>
                    </tr>
                    <tr>
                        <td><span class=\"fontOne\">Inventar Nr.: *</span></td>
                        <td name='inventoryNumber' >".$device->InventoryNumber."</td>
                    </tr>
                    <tr>
                        <td><span class=\"fontOne\">Hersteller: *</span></td>
                        <td name='manufacturer'>".$device->Manufacturer." </td>
                    </tr>
                    <tr>
                        <td><span class=\"fontOne\">Einkaufsdatum: *</span></td>
                        <td name='purchaseDate'>". ConvertDate($device->PurchaseDate, true)."</td>
                    </tr>
                    <tr>
                        <td><span class=\"fontOne\">Garantie: *</span></td>
                        <td name='warranty'>".$device->Warranty." Monate</td>
                    </tr>
                    <tr>
                        <td><span class=\"fontOne\">Serien Nummer: *</span></td>
                        <td name='serialNumber'>" . $device->SerialNumber. " </td>
                    </tr>
                </table> 
            </td>
            <td>
                <span class=\"headlineTwo\">Kontaktinformationen: </span><br>
                    <table class=\"informationTable\" id?=\"supplierInformation\">
                        <tr>
                            <td><span class=\"fontOne\">Firma: *</span></td>
                            <td name='nameSupplier' id=\"nameSuppliertd\">".$device->NameSupplier."</td>
                        </tr>
                        <tr>
                            <td><span class=\"fontOne\">Verk&auml;ufer e-mail: *</span></td>
                            <td name='contactSaleOffice' id=\"contactSaleOfficetd\">".$device->ContactSaleOffice." </td>
                        </tr>
                        <tr>
                            <td><span class=\"fontOne\">Service e-mail: *</span></td>
                            <td name='contactService' id=\"contactServicetd\"> ".$device->ContactService."</td>
                        </tr>
                        <tr>
                            <td><span class=\"fontOne\">Notiz: *</span></td>
                            <td name='noteSupplier' id=\"noteSuppliertd\">".$device->NoteSupplier."</td>
                        </tr>
                        <tr>
                            <td><span class=\"fontOne\">Kontakt Auswahl:</span></td>
                            <td><select onchange='supplierChange(this, $device->SupplierID)' name=\"supplierID\">
                            <option value=\"$device->SupplierID\">".$device->NameSupplier."</option>
";
foreach ($suppliers as $supplier) {
if($supplier['ID'] != $device->SupplierID){
echo"
                            <option value=\"".$supplier['ID']."\">".$supplier['name'] ." </option>
";}}
echo"
                            </select></td>
                        </tr>
                    </table> 
            </td>
        </tr>
        <tr>
            <td>
            <span class=\"headlineTwo\">Standortinformationen: </span><br>
                    <table class=\"informationTable\" >
";if(count($device->RelocationDate)>=1){echo"
                    <tr>
                        <td><span class=\"fontOne\">&Auml;nderung: </span></td>
                        
                        <td>". ConvertDate($device->RelocationDate[0], true)."</td>
                    </tr>
";}if($device->RoomIs != null){echo"
                    <tr>
                        <td><span class=\"fontOne\">Raum ist: </span></td>
                        <td>" .$device->RoomIs[0] . " </td>
                    </tr>
";}if($device->RoomIsNumber != null){echo"
                    <tr>
                        <td><span class=\"fontOne\">Raum Nummer: </span></td>
                        <td >" . $device->RoomIsNumber[0] . "</td>
                    </tr>
";}echo"
                    <tr>
                        <td><span class=\"fontOne\">Raum soll: *</span></td>
                        <td name='roomShould'>";  if($device->RoomShould != null){echo $device->RoomShould[0];} echo"</td>
                    </tr>
";if($device->LevelIs != null){echo"
                    <tr>
                        <td><span class=\"fontOne\">Stockwerk ist: </span></td>
                        <td>" . $device->LevelIs[0] ."</td>
                    </tr>
";
}if($device->LevelShould != null){echo"
                    <tr>
                        <td><span class=\"fontOne\">Stockwerk soll: </span></td>
                        <td>" . $device->LevelShould[0] ."</td>
                        </tr>"
;}echo"
                </table>
            </td> 
            <td>
            <span class=\"headlineTwo\"><br></span>
                <table class=\"informationTable\" >
                    
";echo"
                    <tr>
                        <td><span class=\"fontOne\">MAC Adresse: * </span></td>
                        <td name='macAddress'>";if($device->Mac != null){echo $device->Mac;}echo"</td>
                    </tr>
";if($device->PatchPort != null){echo"                
                    <tr>
                        <td><span class=\"fontOne\">Patch Pannel Port: </span></td>
                        <td>" . $device->PatchPort ."</td>
                    </tr>
";}if($device->Switch != null){echo"                
                    <tr>
                        <td><span class=\"fontOne\">Switch: </span></td>
                        <td>" . $device->Switch ."</td>
                    </tr>
";}if($device->Switchport != null){echo"                
                    <tr>
                        <td><span class=\"fontOne\">Switch Port </span></td>
                        <td>" . $device->Switchport ."</td>
                    </tr>
";}if($device->VlanName != null){echo"                
                    <tr>
                        <td><span class=\"fontOne\">Vlan: </span></td>
                        <td>" . $device->Vlan ."  ". $device->VlanName."</td>
                    </tr>
";}echo"
                    <tr>
                        <td><input type=\"button\" value=\"History\" name=\"roomHistory\" onclick=\"location.href = 'device.php?dev=". $device->ID ."&mode=1';\"/></td>
                        <td></td>
                    </tr>
                </table> 
            </td>
        </tr>    
    </table> 
    <p id=\"submitArea\">
</form>
<form action=\"device.php?dev=$dev\" method=\"POST\">



<table id=\"alertTable\" >
    <tr>
        <td><span class=\"headlineTwo\">Meldungen:</span></td>
        <td></td>
    </tr>
     $deviceChange
</table>



<table id=\"additionTable\">
    <tr>
        <td><span class=\"headlineTwo\">Zusatzinformationen: </span></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td><input type=\"button\" value=\"History\" name=\"additionHistory\" onclick=\"location.href='device.php?dev=". $device->ID ."&mode=2';\"/></td>
        <td><input type=\"submit\" value=\"Hinzuf&uuml;gen\" name=\"additionAdd\" /></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>Typ:</td>
        <td>Information:</td>
        <td>Datum:</td>
        
    </tr>
    <tr>
<datalist id=\"additionTyps\">
";
require '../resources/getConnection.php'; 
$additionTypsClass = new Additions($connection);
$additions = $additionTypsClass->getAllAdditionTyps();
$connection->disconnectDB();
foreach ($additions as $addition) 
    {
        echo"<option value=\"". $addition['type']."\"/>";
    }
echo"
</datalist>
        <td><input list=\"additionTyps\" type=\"text\" value=\"\" onchange=\"changeAdditionInputField(this)\" name=\"addition[0][0]\" id=\"additionTyp0\"  size=\"20px\" /></td>
        <td><textarea rows=\"1\" cols=\"20\" value=\"\" onchange=\"changeAdditionInputField(this)\" name=\"addition[0][1]\" id=\"additionValue0\"  size=\"20px\"/></textarea></td>
        <td><input type=\"datetime-local\" value=\"". date('Y-m-d') ."T".date('H:i:s')."\" onchange=\"changeAdditionInputField(this)\" name=\"addition[0][2]\" id=\"additionDate0\" size=\"20px\"/></td>
        <td></td>
    </tr>
";if(count($device->Additions)>=1)
{
for($i=0; $i< count($device->Additions);$i++){
echo"
    <tr>
        <td><span class=\"fontOne\"> ". $device->Additions[$i][0] .": </span></td>
        <td>". $device->Additions[$i][1][0]."</td>
        <td>". ConvertDateTime($device->Additions[$i][2][0], true) ."</td>
        <td onclick=\"deleteAddition(this, ". $device->Additions[$i][3][0] .",".$device->ID.")\" > <img width=\"20px\" src=\"../design/pictures/delete.png\"> </td>
    </tr>
    ";}}else{
echo"
    <tr>
        <td><br>Keine Zusatz Informationen vorhanden.</td>
    </tr>
";}echo"
</table>   
</form>
"; 
}

//Room History
if($mode == 1)
{
echo"
<a href=\"device.php?dev=". $device->ID ."\">zur&uuml;ck zum Ger&auml;t</a>
<h1>$device->Name Standort History</h1>
<table id=\"list\" >
    <thead>
    <tr>
        <th>Raum Nummer</th>
        <th>Raum Name</th>
        <th>Stockwerk</th>
        <th>Raum Sollte</th>
        <th>Stockwerk Sollte</th>
        <th>&Auml;nderungsdatum</th>
    </tr>
    </thead>
    <tbody>
";

for($i=0; $i < count($device->RelocationDate); $i++)
{
    echo"
        <tr>
            <td>"; if(isset($device->RoomIsNumber[$i])){ echo $device->RoomIsNumber[$i]; }echo"
            </td>
            <td>"; if(isset($device->RoomIs[$i])){echo $device->RoomIs[$i]; } echo"
            </td>
            <td>"; if(isset($device->LevelIs[$i])){echo $device->LevelIs[$i]; } echo "
            </td>
            <td>"; if(isset($device->RoomShould[$i])){echo $device->RoomShould[$i]; } echo"
            </td>
            <td>"; if(isset($device->LevelShould[$i])){echo $device->LevelShould[$i]; } echo"
            </td>
            <td>"; if(isset($device->RelocationDate[$i])){echo ConvertDate($device->RelocationDate[$i], true); } echo"
            </td>
        </tr>
";}echo"
    </tbody>
</table>
";
}

//Addition History
if($mode == 2)
{
 echo"
<a href=\"device.php?dev=". $device->ID ."\">zur&uuml;ck zum Ger&auml;t</a>
<form action=\"device.php?dev=$dev&mode=2\" method=\"POST\">
<h1>$device->Name Zusatzinformationen History</h1>
<table>
    <thead>
        <tr>
        
            <th>Typ</th>
            <th>Information</th>
            <th>Datum</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input list=\"additionTyps\" type=\"text\" value=\"\" onchange=\"changeAdditionInputField(this)\" name=\"addition[0][0]\" id=\"additionTyp0\"  size=\"20px\" /></td>
            <td><textarea rows=\"1\" cols=\"20\" value=\"\" onchange=\"changeAdditionInputField(this)\" name=\"addition[0][1]\" id=\"additionValue0\"  size=\"20px\"/></textarea></td>
            <td><input type=\"datetime-local\" value=\"". date('Y-m-d') ."T".date('H:i:s')."\" onchange=\"changeAdditionInputField(this)\" name=\"addition[0][2]\" id=\"additionDate0\" size=\"20px\"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type=\"submit\" value=\"Hinzuf&uuml;gen\" name=\"additionAdd\" /></td>       
            <td></td>
        </tr>
    </tbody>
</table>
     
 <table id=\"listAddition\" >
    <thead>
        <tr>
        
            <th>Typ</th>
            <th>Information</th>
            <th>Datum</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
";

for($i=0; $i< count($device->Additions);$i++){
    for($j=0; $j < count($device->Additions[$i][1]); $j++)
    {
echo"
        <tr>
            <td>". $device->Additions[$i][0] ."</td>
            <td>". $device->Additions[$i][1][$j] ."</td>
            <td>". ConvertDateTime($device->Additions[$i][2][$j],true) ."</td>
            <td onclick=\"deleteAddition(this, ". $device->Additions[$i][3][$j] .",".$device->ID.")\" > <img width=\"20px\" src=\"../design/pictures/delete.png\"> </td>
        </tr>
";}}echo" 
    </tbody>
</table>
</form>
";
}


require_once '../menu/footer.php';
?>